/*
 * ImportSources module for Netbeans.
 *
 * Copyright (c) 2007, Rogue
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * For queries or bugs, you may contact me via e-mail: roguexz [at] gmail [dot] com
 */
package rogue.netbeans.module.importsources;

import org.openide.WizardDescriptor;

import rogue.netbeans.module.importsources.ui.WizardPanel2UI;

import java.awt.Component;

import javax.swing.JProgressBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * @author Rogue
 */
public class WizardPanel2 extends AbstractWizardPanel
    implements WizardDescriptor.Panel, ChangeListener {
    private boolean panelValid = false;
    private WizardDescriptor wd = null;
    private WizardPanel2UI component;
    private String selectedDirectory = null;
    private String[] fileExtensions = new String[] { "" };

    public Component getComponent() {
        if (component == null) {
            component = new WizardPanel2UI(this);
        }

        return component;
    }

    public boolean isValid() {
        return panelValid;
    }

    public void readSettings(Object settings) {
        // load the parent directory.
        if (settings instanceof WizardDescriptor) {
            wd = (WizardDescriptor) settings;
            selectedDirectory = (String) wd.getProperty(ImportSourcesConstants.SELECTED_DIRECTORY.toString());
            fileExtensions = (String[]) wd.getProperty(ImportSourcesConstants.FILE_EXTENSIONS.toString());

            if (((Boolean) wd.getProperty(ImportSourcesConstants.SCAN_FOR_FILES.toString())).booleanValue()) {
                component.scanFilesInTheBackground(selectedDirectory,
                                                   fileExtensions);
            }
        }
    }

    /**
     * Monitor the JProgressBar for updating the panel state.
     *
     * @param e DOCUMENT ME!
     */
    public void stateChanged(ChangeEvent e) {
        if (e.getSource() instanceof JProgressBar) {
            JProgressBar pb = (JProgressBar) e.getSource();

            if (pb.isIndeterminate()) {
                panelValid = false;

                return;
            }

            if (pb.getValue() == pb.getMaximum()) {
                panelValid = true;
                fireChangeEvent();
            }
        }
    }

    public void storeSettings(Object object) {
        if (object instanceof WizardDescriptor) {
            WizardDescriptor wd = (WizardDescriptor) object;

            // store the selected files to the Wizard Descriptor.
            wd.putProperty(ImportSourcesConstants.SELECTED_FILES.toString(),
                           component.getTree());
            wd.putProperty(ImportSourcesConstants.SCAN_FOR_FILES.toString(),
                           Boolean.FALSE);
        }
    }

    public boolean isPanelProcessingComplete() {
        return panelValid;
    }

    public void terminateProcessing() {
        component.cancelScan();
    }
}
